package com.alstudio.module.xmpp.distributor.versioncheck;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.distributor.distributor.DistributorManager;
import com.alstudio.pojo.module.server.ServerInfo;
import com.alstudio.ui.base.BaseActivity;
import com.alstudio.ui.module.setting.AboutActivity;
import com.alstudio.utils.activity.ALActivityManager;
import com.alstudio.utils.android.net.ALConnectionManager;
import com.alstudio.utils.android.net.ALConnectionManager.ALConnectionType;
import com.alstudio.view.toast.MyToastUtil;

public class VersionManager implements NewVersionListener {

	private static VersionManager instance;

	private boolean isVersionChecked = false;

	private boolean isNewVersionArraived = false;

	public VersionManager() {
		VersionCheckUtils.getInstance().setNewVersionAlertListener(this);
	}

	public static VersionManager getInstance() {
		if (instance == null) {
			instance = new VersionManager();
		}
		return instance;
	}

	public boolean isVersionChecked() {
		return isVersionChecked;
	}

	public boolean isNewVersionArraived() {
		return isNewVersionArraived;
	}

	/**
	 * 检测更新
	 * 
	 * @param currentVersion
	 * @param platform
	 * @param ip
	 * @param port
	 */
	public void checkNewVersion(String currentVersion, String platform,
			String ip, String port) {

		if (!ALLocalEnv.isNetworkConnected())
			return;

		DistributorManager.getInstance().checkNewVersion(currentVersion,
				platform, ip, port);

	}

	@Override
	public void onNewVersionArraived(ServerInfo result) {
		// TODO Auto-generated method stub
		if (result != null) {
			BaseActivity a = ALActivityManager.getALActivityManager()
					.getCurrentActivity();
			if (a == null) {
				return;
			}
			if (ALActivityManager.getALActivityManager().isActivityOnTop(
					AboutActivity.class.getSimpleName())) {
				a.dismissLoadingProgress();
				a.showApkUpdateDialog(result);
			}
		}
	}

	@Override
	public void onConnectSuccess() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onConnectFail(String reason) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCheckNewVersionFailed(String reason) {
		// TODO Auto-generated method stub
		BaseActivity a = ALActivityManager.getALActivityManager()
				.getCurrentActivity();
		if (a == null) {
			return;
		}
		if (ALActivityManager.getALActivityManager().isActivityOnTop(
				AboutActivity.class.getSimpleName())) {
			a.dismissLoadingProgress();
			a.showNoApkUpdateDialog();
		}

	}

}
